import {BMapLibLuShu} from './LuShu.js'
// 轨迹回放
export function Playback(BMap, map) {
    this.BMap = BMap;
    this.map = map;
    this.points = [];
    this.BMapPoints = []; //小车的数组
    this.playMarker = null; // 播放图标
    this.track = null;
    this.playtotal = 0;
    this.followLine = null; // 跟随线
    this.followLinePoints = []; // 跟随线点数组
    this.prev_index= null; // 上一个点索引
    this.clearPoly = []; // 待清空的覆盖物
    // 界限在
    // [低速:0<sudu<=lowSpeed]
    // [正常:lowSpeed<sudu<=overSpeed]
    // [超速:overSpeed<sudu<=(overSpeed*1.5)]
    // [超速(1.5):(overSpeed*1.5)<sudu]
    this.overSpeed = 100;
    this.lowSpeed = 20;
    this.speedColor = {
        low: "#336699", //低速
        normal: "#00FF00", //正常速度颜色
        over: "#F50713", //超速颜色
        over1_5: "#950222" //速度超1.5颜色
    }
    this.speedColorFlag = true;// 跟随线 是否 根据速度变色
}
Playback.prototype.setWheelZoom = function () {
    this.map.enableScrollWheelZoom()
}
Playback.prototype.setPoints = function (points) {
    this.points = points
}
Playback.prototype.getBMapPoint = function (point) {
    return new BMap.Point(point.lng, point.lat)
}
Playback.prototype.setBMapPoints = function (points) {
    var BMapPoints = points.map((item) => {
        return new BMap.Point(item.lng, item.lat)
    })
    this.BMapPoints = BMapPoints
}
Playback.prototype.addMark = function (BMapPoint, opt) {
    opt = opt || {}
    console.log(BMapPoint)
    console.log(opt)
    this.playMarker = new BMap.Marker(BMapPoint, opt)
    this.map.addOverlay(this.playMarker)
}
Playback.prototype.addLine = function (BMapPoints, opt) {
    opt = opt || {
        strokeOpacity: 0.7,
        strokeColor: '#5475d2'
    }
    var polyline = new BMap.Polyline(BMapPoints, opt)
    this.map.addOverlay(polyline)
    return polyline;
}
Playback.prototype.initLushu = function (param) {
    let opts = {
        defaultContent: param.content || "<b>从肥东到肥西</b>",
        speed: param.speed || 1000,
        enableRotation: true,
        landmarkPois: param.landmarkPois || [
            {lng:this.points[1].lng,lat:this.points[1].lat,html:'加油站',pauseTime:1}
            // {lng:this.points[3].lng,lat:this.points[3].lat,html:'高速公路收费站',pauseTime:2}
        ],
        icon: param.icon || new BMap.Icon(
            'http://lbs.baidu.com/jsdemo/img/car.png',
            {
                width: 52,
                height: 26
            },
            {anchor : {width: 27, height: 23}}
            ),
        onFirst: () => {
            // this.initFollowLine();
            this.clearFollowPoly()
        },
        onstop: () => {
            // this.map.removeOverlay(this.track._overlay)
            // this.map.removeOverlay(this.track._marker)
        },
        onMove: (res)=> {
            const {pos, cur} = res
            if(this.speedColorFlag) {
                this.drawSpeedColorLine(pos, cur)
            }else{
                this.followLinePoints.push(pos);
                this.initFollowLine({
                    strokeColor: '#5475d2',
                    strokeOpacity: 0.7
                })
                this.prev_index = null
            }

            if(param.onMove) param.onMove(res)
        },
        onMoveNext: (cur) => {
            // this.followLine = this.addLine([this.BMapPoints[cur], this.])
            if(param.onMoveNext) param.onMoveNext(cur)
        }
    }
    this.playtotal = this.BMapPoints.length;
    this.track = new BMapLibLuShu.LuShu(this.map, this.BMapPoints, this.points, opts)
    // 完整轨迹线
    this.trackLineInit({
        strokeColor: param.strokeColor
    })
    // 添加覆盖物 起始点等
    this.trackMarkerInit()
    // 跟随线初始化
    // this.initFollowLine()
}
Playback.prototype.play = function () {
    this.track.start();
}
Playback.prototype.pause = function () {
    this.track.pause();
}
Playback.prototype.stop = function () {
    this.track.stop();
}
Playback.prototype.dispose = function () {
    this.track.dispose();
}
Playback.prototype.trackLineInit = function (param) {
    var symbol = new BMap.Symbol(BMap_Symbol_SHAPE_FORWARD_OPEN_ARROW, {
        scale: 0.5,//图标缩放大小
        strokeColor:'#fff',//设置矢量图标的线填充颜色
        strokeWeight: '1.5',//设置线宽
    })
    var icons = new BMap.IconSequence(symbol, '10', '30')
    this.addLine(this.BMapPoints, {
        strokeColor: param.strokeColor || 'grey',
        strokeOpacity: 0.4,
        icons: [icons]
    })
}
// 轨迹添加覆盖物
Playback.prototype.trackMarkerInit = function() {
    var startPoint = this.BMapPoints[0];
    var endPoint = this.BMapPoints[this.BMapPoints.length - 1];
    var startIcon = require('@/assets/images/markerStart.png')
    var endIcon = require('@/assets/images/markerEnd.png')
    console.log(startIcon)
    this.addMark(startPoint, {
        icon: new BMap.Icon(startIcon, {width:72,height: 130}, {
        }),

    })
    this.addMark(endPoint,{
        icon: new BMap.Icon(endIcon, {width:72,height: 130})
    })
}
// 手动按进度播放
Playback.prototype.manualSetProcess = function (val) {
    // 重置画跟随线索引
    this.prev_index = null;
    // 按进度播放
    let process = Math.floor((val/100)*(this.playtotal));
    this.track.i = process - 1 >= 0 ? process - 1: 0;

    this.track.pause();
    this.clearFollowPoly()
    this.track.start();
}
// 获取进度
Playback.prototype.getProcess = function (cur) {
    let runTotal = this.playtotal;
    let process = Math.floor(((cur+1)/runTotal)*100);
    return process;
}
// 初始化跟随线
Playback.prototype.initFollowLine = function (opts) {
    opts = opts || {}
    if(this.followLine) {
        this.followLine.setPath(this.followLinePoints)
    }else{
        console.log('init');
        this.followLine = this.addLine(this.followLinePoints, opts)
        this.clearPoly.push(this.followLine)
    }
}
// 重置跟随线
Playback.prototype.resetFollowLine = function () {
    this.followLinePoints = []
    this.followLine = null
}
// 去除跟随线
Playback.prototype.clearFollowPoly = function () {
    if(this.clearPoly && this.clearPoly.length>0) {
        this.clearPoly.forEach(item => {
            this.map.removeOverlay(item);
        })
    }
    // if(!this.speedColorFlag) {
    //
    // }
    if(this.followLine) this.map.removeOverlay(this.followLine);
    this.resetFollowLine();
    this.clearPoly = [];
}
// 跟随线-速度颜色
Playback.prototype.getSpeedColor = function (speed) {
    var color = this.speedColor['normal']
    if (speed > 0 && speed <= this.lowSpeed) {
        color = this.speedColor['low']
    } else if (speed > this.lowSpeed && speed <= this.overSpeed) {
        color = this.speedColor['normal']
    } else if (speed > this.overSpeed && speed <= (this.overSpeed * 1.5)) {
        color = this.speedColor['over']
    } else if (speed > (this.overSpeed * 1.5)) {
        color = this.speedColor['over1_5']
    }
    return color;
}

/***
 * 根据速度画不同颜色线
 * @param pos 当前点 细化经纬度
 * @param cur 当前点 索引
 */
Playback.prototype.drawSpeedColorLine = function (pos, cur) {
    if(this.prev_index == null || cur != this.prev_index) { // 刚开始 || 下一段
        this.resetFollowLine()
        this.followLinePoints.push(pos);
        let speed = this.points[cur].speed;
        let _color = this.getSpeedColor(speed)
        this.initFollowLine({
            strokeColor: _color
        })
    }else if(cur == this.prev_index){ // 一段之间
        this.followLinePoints.push(pos);
        if(this.followLine) this.followLine.setPath(this.followLinePoints)
    }
    this.prev_index = cur
}

// // 播放(方式一：设置marker位置)
// Playback.prototype.play = function () {
//     console.log(this.playIndex);
//     console.log(this.BMapPoints.length);
//     // 已过的轨迹
//     if(this.playIndex>0){
//         this.addLine([this.BMapPoints[this.playIndex-1], this.BMapPoints[this.playIndex]], {
//             strokeColor: '#5475d2',
//         })
//     }
//     //设置点位置
//     this.playMarker.setPosition(this.BMapPoints[this.playIndex])
//     if(this.playIndex < this.BMapPoints.length-1) {
//         console.log('hhh');
//         this.playIndex++;
//         this.playTimer = setTimeout(()=> {
//             this.play()
//         },this.playDelay);
//     }else{
//         clearTimeout(this.playTimer)
//     }
// }
// Playback.prototype.pause = function () {
//     clearTimeout(this.playTimer)
// }
//
// Playback.prototype.stop = function () {
//     clearTimeout(this.playTimer)
//     this.playIndex = 0
//     this.playMarker.setPosition(this.BMapPoints[this.playIndex])
// }